package cn.momoshenchi.medicine.controller;

import cn.momoshenchi.medicine.enumerate.Add;
import cn.momoshenchi.medicine.enumerate.Update;
import cn.momoshenchi.medicine.pojo.BaseDoctor;
import cn.momoshenchi.medicine.pojo.BaseDrug;
import cn.momoshenchi.medicine.pojo.BaseOrganization;
import cn.momoshenchi.medicine.service.BaseOrganizationService;
import cn.momoshenchi.medicine.util.AssertUtil;
import cn.momoshenchi.medicine.util.PageUtil;
import cn.momoshenchi.medicine.util.ResultInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @version : 1.0
 * @author: momoshenchi
 * @date: 2021/7/16 - 9:08
 */
@RestController
@RequestMapping("/organization")
public class OrganizationController
{
    @Autowired
    private BaseOrganizationService organizationService;

    @GetMapping("/page")
    public ResultInfo getOrganization(PageUtil page)
    {
        IPage<BaseOrganization> organizationPage =new Page<>(page.getPage(), page.getSize());
        organizationPage = organizationService.page(organizationPage);
        return  new ResultInfo(organizationPage);
    }

    @PostMapping("/add")
    public  ResultInfo add(@RequestBody  @Validated(Add.class) BaseOrganization organization)
    {
        AssertUtil.isTrue(!organizationService.save(organization),"添加失败");
        return  new ResultInfo();
    }

    @PostMapping("/update")
    public  ResultInfo update(@RequestBody @Validated(Update.class) BaseOrganization organization)
    {
        AssertUtil.isTrue(!organizationService.updateById(organization),"更新失败");
        return  new ResultInfo();
    }

    @PostMapping("/delete/{id}")
    public  ResultInfo delete(@PathVariable String id)
    {
        AssertUtil.isTrue(!organizationService.removeById(id),"删除失败");
        return  new ResultInfo();
    }

    @GetMapping("/get/{id}")
    public  ResultInfo get(@PathVariable String id)
    {
        BaseOrganization baseOrganization = organizationService.getById(id);
        return  new ResultInfo(baseOrganization);
    }
}
